import tornado
import tornado.web
from tornado.websocket import WebSocketHandler
conndict={}
from mysql import db
from utils.urls import BaseHandler
import pymongo,json
from mysql import db
class Getall(BaseHandler):
    def get(self):
        sql="""select * from greenhouse"""
        res=db.findAll(sql)
        self.finish({"code":200,'msg':res})

# def jianting():
#     sql="""select * from greenhouse where value>max_value or value<low_value;"""
#     res=db.findAll(sql)
#     print("ress",res)
#     list=[]
#     list2=[]
#     for i in res:
#         list.append(i['id'])
#         list2.append(i['value'])
#     print("list1>>>>>>",list)
#     print("list2>>>>>>",list2)
#     data={"list":list,"list2":list2}
#     WebSocketHandler.on_message(str(data))
# jianting()


class WebsockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    #建立连接
    def open(self):
        #获取参数
        id = int(self.get_query_argument("id"))
        print("12345")
        #存储链接
        conndict[id]=self

    #接收消息
    def on_message(self,mes):
        print(mes)
        self.write_message(mes)

    def on_close(self):
        pass
import time
from my_redis import r

class ZZhandler(tornado.web.RequestHandler):
    def get(self):
        while True:
            time.sleep(5)
            id = int(self.get_query_argument("id"))
            wendu = r.list_lpop('key1')
            print(wendu,'>>>>>>>>>>>>>')
            if wendu:
                print(wendu)
                # print(res1)
                # print(res2)
                sql = "select * from greenhouse where id=1"
                res = db.findone(sql)
                print(res)
                rlist = []
                if int(wendu.decode()) > int(res['max']):
                    print("大了，状态为1")
                    status = 1
                    rlist.append({'id':res['id'],'name':res['name'],'wendu':wendu.decode(),'min':res['min'],'max':res['max'],'status':status})
                    print("rlist", rlist)
                    conndict[id].write_message(json.dumps(rlist))
                if int(wendu.decode()) < int(res['min']):
                    print("小了，状态为2")
                    status = 2
                    rlist.append({'id': res['id'], 'name': res['name'], 'wendu': wendu.decode(), 'min': res['min'], 'max': res['max'],'status': status})
                    print("rlist",rlist)
                    conndict[id].write_message(json.dumps(rlist))

                if int(wendu.decode())>res['min'] and int(wendu.decode())<res['max']:
                    print("正好，状态为0")
                    status = 0
                    rlist.append({'id': res['id'], 'name': res['name'], 'wendu': wendu.decode(), 'min': res['min'], 'max': res['max'], 'status': status})
                    print("rlist", rlist)
                    conndict[id].write_message(json.dumps(rlist))

            else:
                conndict[id].write_message("啥也没有")

class addredis(BaseHandler):
    def get(self):
        wendu=self.get_query_argument("wendu")
        r.list_push("key1",wendu)
        self.finish({"code":200,'msg':'添加成功'})

 # lpush key1 12
 # lpush key1 22
 # lpush key1 43
 # lpush key1 99
 # lpush key1 234
 # lpush key1 1
 # lpush key1 2
 # lpush key1 4
 # lpush key1 33
 # lpush key1 3
 # lpush key1 111